package net.orange.framework.validator;

import java.util.Collection;

import net.orange.architecture.process.ProcessFactory;
import net.orange.architecture.validator.IObjectValidator;
import net.orange.framework.logic.IDepartmentProcess;
import net.orange.framework.model.Department;

/**
 * Validation : If the department has reference by other department as parent,
 * so It can't be deleted.
 */
public class DepartmentHasChildrenValidator extends FrameworkValidator
		implements IObjectValidator {

	/**
	 * The target department.
	 */
	private Department department;

	/**
	 * Disabled the default contractor.
	 * 
	 * @exception Exception
	 */
	@SuppressWarnings("unused")
	private DepartmentHasChildrenValidator() throws Exception {

	}

	/**
	 * Constructor with department object and DAO object to initialize the
	 * validation.
	 * 
	 * @param department
	 *            The target department.
	 * @exception Exception
	 */
	public DepartmentHasChildrenValidator(Department department)
			throws Exception {
		this.department = department;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.orange.architecture.validator.ObjectValidator#check()
	 */
	public boolean check() throws Exception {

		if (department == null || department.getDept_code() == null
				|| department.getDept_code().length() <= 0)
			return true;

		IDepartmentProcess process = (IDepartmentProcess) ProcessFactory
				.get(Department.class);
		Collection childs = process.findSubDepartments(department.getPk());

		return (childs == null || childs.size() <= 0);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.orange.architecture.validator.ObjectValidator#getMessage()
	 */
	public String getMessage() {
		return department.getDept_name() + message.get("error2_dept_04");
	}
}
